package fr.vvins.core.services;

import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.Path;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import fr.vvins.core.biz.exception.BizException;
import fr.vvins.core.biz.group.GroupException;
import fr.vvins.core.biz.group.GroupManager;
import fr.vvins.core.biz.group.dto.GroupDto;
import fr.vvins.core.biz.group.dto.GroupDtoInfo;
import fr.vvins.core.biz.user.UserManager;
import fr.vvins.core.common.TechnicalException;
import fr.vvins.core.order.entity.PaymentMethod;

@Path("/group")
@Component("groupService")
public class GroupServiceImpl implements GroupService {
	
	@Resource(name="groupManager")
	public GroupManager groupManager;
	@Resource(name="userManager")
	public UserManager userManager;
	
	@Override
	public GroupDto createGroup(String token, GroupDto group) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(token)){
			throw new GroupException(GroupException.ID_EMPTY, "Vigneron id should be empty");
		}
		Integer userId = userManager.decrypt(token);
		if(group.getCreator()== null || !userId.equals(group.getCreator().getId())){
			throw new GroupException(GroupException.CREATOR, "Creator is not same as current user.");
		}
		return groupManager.createGroup(group);
	}

	@Override
	public GroupDto getGroup(String id) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(id)){
			throw new GroupException(GroupException.ID_EMPTY, "Group id should not be empty");
		}
		try{
			Integer.parseInt(id);
		} catch (NumberFormatException e) {
			throw new GroupException(GroupException.ID_FORMAT, "id should be an integer");
		}
		return groupManager.getGroup(Integer.parseInt(id));
	}

	@Override
	public List<GroupDtoInfo> getGroupByVigneron(String vigneronId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(vigneronId)){
			throw new GroupException(GroupException.ID_EMPTY, "Vigneron id should not be empty");
		}
		try{
			Integer.parseInt(vigneronId);
		} catch (NumberFormatException e) {
			throw new GroupException(GroupException.ID_FORMAT, "Vigneron id should be an integer");
		}
		return groupManager.getGroupByVigneron(Integer.parseInt(vigneronId));
	}
	
	@Override
	public List<GroupDtoInfo> getGroups() throws BizException, TechnicalException {
		return groupManager.getGroups();
	}

	@Override
	public PaymentMethod getPaymentMethodGroup(String id) throws BizException, TechnicalException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<GroupDtoInfo> getGroupByCreator(String creatorId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(creatorId)){
			throw new GroupException(GroupException.ID_EMPTY, "Creator id should not be empty");
		}
		try{
			Integer.parseInt(creatorId);
		} catch (NumberFormatException e) {
			throw new GroupException(GroupException.ID_FORMAT, "Creator id should be an integer");
		}
		return groupManager.getGroupByCreator(Integer.parseInt(creatorId));
	}

	@Override
	public List<GroupDtoInfo> getGroupByUser(String userId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(userId)){
			throw new GroupException(GroupException.ID_EMPTY, "User id should not be empty");
		}
		try{
			Integer.parseInt(userId);
		} catch (NumberFormatException e) {
			throw new GroupException(GroupException.ID_FORMAT, "User id should be an integer");
		}
		return groupManager.getGroupByUser(Integer.parseInt(userId));
	}

}
